package vn.com.bookstore.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import vn.com.bookstore.domain.model.Account;
import vn.com.bookstore.domain.model.Customer;
import vn.com.bookstore.model.vo.CustomerVO;
import vn.com.bookstore.service.CustomerService;
import vn.com.bookstore.utils.CardHelper;

@Controller
public class MobileCartConttroller {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(MobileCartConttroller.class);

	@Autowired
	private CustomerService customerService;

	@RequestMapping(value = "/online/nap-tien", method = RequestMethod.GET)
	public String mobileCard(Model model, HttpServletRequest request) {
		LOGGER.info("mobileCard()");
		SecurityContext context = SecurityContextHolder.getContext();
		String username = context.getAuthentication().getName();
		if (username == null || username.equals("anonymousUser")) {
			return "redirect:/online/home";
		}
		CustomerVO customerVO = customerService
				.getCustomerVOByUsername(username);
		Account account = customerService.getAccountById(customerVO
				.getAccountId());
		model.addAttribute("account", account);
		model.addAttribute("customerVO", customerVO);
		CardHelper cardHelper = new CardHelper();
		String nganluongURL = cardHelper.excutePost("http://pavibook.vn:88",
				null, "GET");
		model.addAttribute("nganluongURL", nganluongURL.trim());
		String[] splitResult = nganluongURL.split("&");
		String tokenCode = splitResult[2];
		tokenCode = tokenCode.replace("token_code=", "");
		HttpSession session = request.getSession();
		session.setAttribute("TOKEN_CODE", tokenCode);
		
//		System.out.println("TOKEN_CODE: "+session.getAttribute("TOKEN_CODE"));
		return "website/mobilecard/mobilecard";
	}

	@RequestMapping(value = "/online/xu-ly-nap-tien", method = RequestMethod.GET)
	public String processMobileCard(Model model, HttpServletRequest request,
			@RequestParam("merchant_site_code") String merchantSiteCode,
			@RequestParam("receiver") String receiver,
			@RequestParam("order_code") String orderCode,
			@RequestParam("amount") int amount,
			@RequestParam("currency_code") String currencyCode,
			@RequestParam("token_code") String tokenCode,
			@RequestParam("checksum") String checksum ){
		LOGGER.info("processMobileCard()");
		SecurityContext context = SecurityContextHolder.getContext();
		String username = context.getAuthentication().getName();
		if (username == null || username.equals("anonymousUser")) {
			return "redirect:/online/home";
		}
		HttpSession session = request.getSession();
		String sessionTokenCode = (String) session.getAttribute("TOKEN_CODE");
		
		if(!tokenCode.equals(sessionTokenCode) || !merchantSiteCode.equals(CardHelper.MERCHANT_ID)){
			model.addAttribute("messageProcess", 0);
		}else{
			Customer customer = customerService.getCustomerByUsername(username);
			customer.setPointCart(customer.getPointCart() + amount);
			customerService.updateAmountOfCustomer(customer);
			
			model.addAttribute("messageProcess", amount);
			
			session.setAttribute("TOKEN_CODE", null);
		}
		
		CustomerVO customerVO = customerService
				.getCustomerVOByUsername(username);
		Account account = customerService.getAccountById(customerVO
				.getAccountId());
		model.addAttribute("account", account);
		model.addAttribute("customerVO", customerVO);

		return "website/mobilecard/mobilecard";
	}

}
